<template>
  <div class="app-container">
    <!-- 分页查询 -->
    <el-form
      ref="queryParams"
      :model="queryParams"
      size="small"
      label-suffix=":"
      label-width="150px"
    >
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-form-item
            :label="$t('containerLang.containerCode')"
            prop="containerCode"
          >
            <el-input
              v-model="queryParams.containerCode"
              :placeholder="$t('containerLang.containerCode')"
              clearable
              style="width: 200px"
            />
          </el-form-item>
        </el-col>
        <el-col :span="1.5">
          <el-form-item :label="$t('depotLang.depot')" prop="depotId">
            <el-select
              v-model="queryParams.depotId"
              clearable
              style="width: 200px"
              @change="changeDepot"
            >
              <el-option
                v-for="item in depotOptionList"
                :key="item.value"
                :value="item.value"
                :label="$t(item.title)"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-form-item :label="$t('statusLang.status')" prop="containerStatus">
            <el-select
              v-model="queryParams.containerStatus"
              clearable
              style="width: 200px"
            >
              <el-option
                v-for="item in containerStatusOptions"
                :key="item.value"
                :value="item.value"
                :label="$t('statusLang.' + item.title)"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="1.5">
          <el-form-item
            :label="$t('containerLang.containerType')"
            prop="depotContainerTypeId"
          >
            <el-select
              v-model="queryParams.depotContainerTypeId"
              clearable
              style="width: 200px"
            >
              <el-option
                v-for="item in depotContainerTypeList"
                :key="item.value"
                :value="item.value"
                :label="$t(item.title)"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            size="small"
            icon="el-icon-search"
            @click="handleQuery"
          >
            {{ $t("query") }}
          </el-button>
          <el-button
            type="primary"
            size="small"
            icon="el-icon-refresh-left"
            @click="handleReset"
          >
            {{ $t("reset") }}
          </el-button>
        </el-col>
      </el-row>
    </el-form>
    <!-- 操作栏 -->
    <el-row :gutter="10">
      <el-col :span="1.5">
        <el-button
          type="primary"
          size="small"
          icon="el-icon-plus"
          @click="handleAdd"
        >
          {{ $t("add") }}
        </el-button>
        <el-button
          type="plain"
          size="small"
          icon="el-icon-printer"
          @click="print"
        >
          {{ $t("print") }}
        </el-button>
      </el-col>
    </el-row>
    <!-- 数据列表 -->
    <el-table
      ref="table"
      v-loading="loading"
      :data="tableData"
      border
      class="mt-10"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" align="center" min-width="20" />
      <el-table-column
        :label="$t('tableIndex')"
        type="index"
        :index="indexAdd"
        align="center"
        width="80"
      />
      <el-table-column
        :label="$t('containerLang.containerCode')"
        prop="containerCode"
        align="center"
        min-width="160"
      />
      <el-table-column
        :label="$t('depotLang.depotCodeAndName')"
        prop="depotCodeAndName"
        align="center"
        min-width="120"
      />
      <el-table-column
        :label="$t('statusLang.status')"
        prop="containerStatus"
        align="center"
        min-width="120"
      >
        <template v-slot="{ row }">
          <el-tag :type="containerStatusTag[row.containerStatus]">
            {{
              $t(
                "statusLang." +
                  getTitleByValue(containerStatusOptions, row.containerStatus)
              )
            }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('containerLang.containerTypeCodeAndName')"
        prop="containerTypeCodeAndName"
        align="center"
        min-width="120"
      />
      <el-table-column
        :label="
          $t('unitLang.length') +
            '*' +
            $t('unitLang.width') +
            '*' +
            $t('unitLang.height') +
            '(CM)'
        "
        prop="containerSize"
        align="center"
        min-width="120"
      />

      <el-table-column
        :label="$t('remark')"
        prop="remark"
        align="center"
        min-width="120"
      />
      <el-table-column
        :label="$t('gmtModified')"
        prop="gmtModified"
        align="center"
        min-width="140"
      />
      <el-table-column
        :label="$t('operation')"
        width="150"
        align="center"
        fixed="right"
      >
        <template v-slot="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ $t("edit") }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ $t("delete") }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 页码 -->
    <el-row type="flex" justify="end" align="middle" style="height: 60px">
      <el-pagination
        :total="total"
        layoout="total,prev,pager,next"
        :current-page="queryParams.pageNo"
        :page-size="queryParams.pageSize"
        @current-change="pageNoChange"
      />
    </el-row>
    <!-- 表单 -->
    <info-form
      v-model="infoParams.id"
      :visible="containerDialog"
      :operation="infoParams.operation"
      :title="title"
      @close="infoClose"
    />
    <vue-easy-print
      v-show="printDialog"
      ref="easyPrint"
      table-show
      :one-page-row="onePageRow"
    >
      <template v-for="(item, i) in multipleSelection">
        <vue-barcode
          :key="i * Math.random() + 1"
          line-color="#000"
          display-value="true"
          format="CODE128"
          font="bold"
          :value="item.containerCode"
          text-align="center"
          text-position="bottom"
          text-margin="5"
          font-size="20"
          height="100"
          width="2"
          class="barCode"
          style="page-break-after: always;"
        />
      </template>
    </vue-easy-print>
  </div>
</template>

<script>
import VueBarcode from 'vue-barcode'
import vueEasyPrint from 'vue-easy-print'
import InfoForm from './components/info-form.vue'
import { getDictOptionList, getTitleByValue } from '@/utils/dictUtils'
import { containerStatusDict } from '@/utils/dictData'
import { getDepotListAllStatus } from '@/api/business/depotManage/depot'
import {
  getDepotContainerPageList,
  deleteDepotContainer
} from '@/api/business/depotManage/container'
import { getDepotContainerTypeAllStatus } from '@/api/business/depotManage/containerType'
export default {
  name: '',
  components: { InfoForm, VueBarcode, vueEasyPrint },
  data() {
    return {
      depotOptionList: [],
      depotContainerTypeList: [], // 流转箱类型下拉
      containerStatusDict: containerStatusDict,
      getTitleByValue: getTitleByValue,
      containerStatusTag: ['warning', 'success'],
      containerStatusOptions: getDictOptionList(containerStatusDict), // 状态下拉
      queryParams: {
        pageSize: 10,
        pageNo: 1,
        containerCode: null,
        depotId: null,
        depotContainerTypeId: null,
        containerStatus: null
      },
      tableData: [],
      total: 0,
      loading: false,
      containerDialog: false,
      title: '',
      infoParams: {
        id: null,
        operation: null
      },
      multipleSelection: [], // el-table多选选中的值
      printDialog: false,
      onePageRow: 1
    }
  },
  created() {
    this.getDepotOptionList()
    this.getDepotContainerPageList()
    this.changeDepot('')
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    async print() {
      if (this.multipleSelection.length === 0) {
        const flag = await this.$confirm(
          this.$t('locationLang.pleaseSelectContainer'),
          this.$t('confirmHint'),
          {
            confirmButtonText: this.$t('confirm'),
            cancelButtonText: this.$t('cancel'),
            type: 'warning'
          }
        ).catch((err) => err)
        if (flag === 'cancel') return
        else return
      }
      this.$refs.easyPrint.print()
    },
    // 流转箱类型下拉
    async changeDepot(value) {
      this.queryParams.depotContainerTypeId = null
      console.log(value)
      // 根据选择的仓库id ==> 流转箱类型下拉
      const res = await getDepotContainerTypeAllStatus({
        depotId: value,
        modifiedUser: 0
      })
      console.log(res, '流转箱')
      this.depotContainerTypeList = res.data
    },
    // 仓库下拉
    async getDepotOptionList() {
      const res = await getDepotListAllStatus()
      if (!res) return
      this.depotOptionList = res.data || []
    },
    // 流转箱列表
    async getDepotContainerPageList() {
      const { data } = await getDepotContainerPageList({ ...this.queryParams })
      this.tableData = data.containerInfoDTOList
      // console.log(this.tableData);
      this.total = data.totalRowNum
    },
    handleQuery() {
      this.loading = true
      this.queryParams.pageNo = 1
      this.getDepotContainerPageList()
      this.loading = false
    },
    async handleReset() {
      await this.$refs.queryParams.resetFields()
      this.getDepotContainerPageList()
    },
    handleAdd() {
      this.infoParams.operation = 1
      this.title = this.$t('add')
      this.containerDialog = true
    },
    handleEdit(row) {
      this.infoParams.operation = 2
      this.infoParams.id = row.containerId
      this.title = this.$t('edit')
      this.$nextTick(() => {
        this.containerDialog = true
      }, 400)
    },
    async handleDelete(row) {
      const flag = await this.$confirm(
        this.$t('delete') + '?',
        this.$t('hint'),
        {
          type: 'warning'
        }
      ).catch((err) => err)
      if (flag === 'cancel') return

      const res = await deleteDepotContainer({
        containerId: row.containerId,
        modifiedUser: 0
      })
      if (res.returnCode !== 10000) return
      this.$message({
        type: 'success',
        message: this.$t('handleSuccess')
      })
      this.getDepotContainerPageList()
    },
    pageNoChange(newPage) {
      this.queryParams.pageNo = newPage
      this.getDepotContainerPageList()
    },
    indexAdd(index) {
      // console.log(index)
      const page = this.queryParams.pageNo // 当前页码
      const pagesize = this.queryParams.pageSize // 每页条数
      return index + 1 + (page - 1) * pagesize
    },
    infoClose() {
      this.containerDialog = false
      this.getDepotContainerPageList()
      setTimeout(() => {
        this.infoParams = {
          id: null,
          operation: 0
        }
      })
    }
  }
}
</script>

<style></style>
